<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<title>武汉理工大学出版社</title>
		<script src="js/jquery-3.2.1.min.js"></script>
		<script src="js/rem.js"></script>
		<link rel="stylesheet" href="css/reset.css">
		<link rel="stylesheet" href="css/page_div.css">
		<script src="js/v3.2.8/vue.global.prod.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/axios.min.js"></script>
		<link rel="stylesheet" href="css/style.css">
	</head>

	<body style="background-color: #fff;">
		<!-- 头部 -->
		<div id="header"></div>
		
		<div id="bootstrap"></div>
		
		<div class="sales_zone" id="app">
			<div class="sales_zone_nav">
				<div class="title">
					<div>
						<p class="p1">资源下载</p>
						<p class="p2">DIGITAL SERVICE</p>
					</div>
				</div>
				<div class="nav_list">
					<ul>
						<li v-for="(item,index) in salesZoneList" @click="onSalesZone(index,item.name)" :style="{'background': szCur==index?'#b3161d':''}">
							<div class="nav_list_left">
								<img v-if="szCur==index" class="img1" :src="szCur==index?'static/xzIcon1.png':''">
								<span :style="{'color': szCur==index?'#fff':''}">{{item.name}}</span>
							</div>
							<img class="img2" :src="szCur==index?'static/xzIcon2.png':'static/wxzIcon.png'"/>
						</li>
					</ul>
				</div>
			</div>
			
			<div class="sales_zone_cont">
				<!-- 类目标题 -->
				<div class="sales_zone_cont_title">
					<div class="sales_zone_cont_titles">
						<img src="static/titleIcon.png"/>
						<p>{{szName}}</p>
					</div>
					<img class="sales_zone_cont_title_back" src="static/titleBack.png">
				</div>
				<!-- 数字产品 -->
				<div v-if="szCur==0">
					<div class="book_list" >
						<ul>
							<li v-for="(item,index) in digitalProducts" @click="onDetails(item.id)">
								<img :src="imgUrl+item.coverImg">
								<div class="content">
									<p class="title">{{item.bookName}}</p>
									<p class="text">载体：{{item.carrier}}</p>
									<p class="text">ISBN：{{item.isbn}}</p>
									<p class="text">出版时间：{{item.publishTime}}</p>
									<p class="text" style="margin: 0;">定价：<span>￥{{item.originalPrice}}</span></p>
								</div>
							</li>
						</ul>

					</div>
					<div v-cloak class="app2">
						<div v-show="total >= 1" class="pageContainer">
							<ul class="pagesInner">
<!--								<li class="page" @click="prevOrNext(-1,szCur)"><span aria-hidden="true">&lt;</span></li>-->
								<li class="page" v-for="(item, index) in pages" :key="index" :class="{actived: item == pageIndex}" @click="selectPage(item,szCur)">
									<span>{{item}}</span>
								</li>
<!--								<li class="page" @click="prevOrNext(1,szCur)"><span class="fa fa-chevron-right" aria-hidden="true">&gt;</span></li>-->
							</ul>
							共{{total1}}页&nbsp;转到第&nbsp;
							<input type="text" v-model="goToPage" class="totalinput">
							&nbsp;页&nbsp;
							<button @click="handleGoToPage(szCur)">GO</button>
						</div>
					</div>
				</div>
				
				<!-- 资源下载 -->
				<div  v-else>
				<div class="book_list">
					<ul>
						<li v-for="(item,index) in bookLists">
							<img :src="imgUrl+item.coverImg">
							<div class="content">
								<p class="title">{{item.bookName}}
									<span class="text" style="font-weight: 100;margin-bottom: 0;">更新时间：{{item.updateTime}}</span>
								</p>
								<p class="text">作者：{{item.bookAuthor}}</p>
								<p class="text">出版时间：{{item.publishTime}}</p>
								<p class="text">出版社：{{item.publishHouse}}</p>
								<p class="text">ISBN：{{item.isbn}}</p>
								<p class="b3" v-if="item.digitalProductUrl" @click="downloadFile(imgUrl+item.digitalProductUrl,item.bookName)">点击下载</p>
							</div>
						</li>
					</ul>
				</div>
				<div v-cloak class="app2">
					<div v-show="total >= 1" class="pageContainer">
						<ul class="pagesInner">
<!--							<li class="page" @click="prevOrNext(-1,szCur)"><span aria-hidden="true">&lt;</span></li>-->
							<li class="page" v-for="(item, index) in pages" :key="index" :class="{actived: item == pageIndex}" @click="selectPage(item,szCur)">
								<span>{{item}}</span>
							</li>
<!--							<li class="page" @click="prevOrNext(1,szCur)"><span class="fa fa-chevron-right" aria-hidden="true">&gt;</span></li>-->
						</ul>
						共{{total2}}页&nbsp;转到第&nbsp;
						<input type="text" v-model="goToPage" class="totalinput">
						&nbsp;页&nbsp;
						<button @click="handleGoToPage(szCur)">GO</button>
					</div>
				</div>
				</div>
				<!-- <div id="page" class="page_div"></div> -->
			</div>
			
			<!-- 加载中 -->
			<div class="load_page" v-show="isLoadingShow">
				<div class="content">
					<div class="item">
						<i class="loader --1"></i>
					</div>
				</div>
			</div>
		</div>
		
		
		<!-- 底部 -->
		<div id="footer"></div>

	</body>
	<script>
		var name = window.location.href.split("&")[0].split("=")[1];
		if(name=='ZYXZ'){
			title = 1;
		}else{
			title = 0
		}
		const App = {
			data() {
				return {
					pageIndex: 1,
					pageSize: 10,
					goToPage: '',
					total:1,
					total1:1,
					total2:1,
					imgUrl:'http://dbs.wutp.com.cn/CM-API',
					szCur:title,
					szName:title==0?'数字产品':'资源下载',
					salesZoneList:[
						{
							name:'数字产品'
						},
						{
							name:'资源下载'
						}
					],
					
					bookLists:[],
					digitalProducts:[],
					isLoadingShow:false
				}
			},
			computed: {
				pages: function () {
					var t
					if(this.szCur == 0){
						t = this.total1
					}else if(this.szCur == 1){
						t = this.total2
					}
					console.log("页码总数",t)
					// 每次最多显示5个页码
					var c = this.pageIndex
					var p = []
					for (var i = 1; i <= t; i++) {
						p.push(i)
					}
					var l = p.length
					if (l <= 5) { // 总页数<=5，显示全部页码
						return p
					} else if (l > 5 && c <= 4) { // 总页数>5 && 当前页面<=3
						return [1, 2, 3, 4, 5,'...', '尾页']
					} else if (l > 5&& c > 4 && c <= l - 3) { // 总页数 > 5 && 当前页面 > 3 && 当前页 < 总页数 - 3
						return [c - 3, c - 2, c - 1, c, c + 1,  '...', '尾页']
					} else { // 总页数 > 5 && 当前页面 > 3 && 当前页 > 总页数 - 3
						debugger
						return [ '首页',t - 3, t - 2, t - 1,t ]
					}
				},
			},
			mounted(){
				sessionStorage.setItem('href',window.location.href);
				this.isLoadingShow = true
				this.getBooksForSales();
				this.getDigitalProducts();
			},
			methods:{
				onDetails(id){
					let hrefs = window.location.href;
					window.location.href="reDetails.html?name=SZFW&id="+id+'&href='+hrefs ;
				},
				onSalesZone(e,n){
					this.szCur = e;
					this.szName = n;
					this.pageIndex = '1'
					this.pageSize = '10'
					this.goToPage = ''
				},
				//数字产品
				async getDigitalProducts(){
					let that = this;
					const params = {
						pageNum: that.pageIndex,
						pageSize: that.pageSize
					}
					await axios({
						method:"get",
						url:"http://dbs.wutp.com.cn/CM-API/resourceDownloadApi/getDigitalProducta",
						params : params,
					}).then(function (res) {
						that.digitalProducts = res.data.data.content;

						// 总条数除以10并向上取整得到总页数
						const total = res.data.data.total / that.pageSize;
						that.total1 = Math.ceil(total);
						// that.total = res.data.data.total;
						that.pageIndex = res.data.data.pageNum
						that.pageSize = res.data.data.pageSize;
						that.isLoadingShow = false

					})
				},
				//资源下载
				async getBooksForSales(){
					let that = this;
					const params = {
						pageNum: that.pageIndex,
						pageSize: that.pageSize
					}
					await axios({
						method:"get",
						url:"http://dbs.wutp.com.cn/CM-API/resourceDownloadApi/getBooksForSales",
						params : params,
					}).then(function (res) {
						that.bookLists = res.data.data.content;

						// 总条数除以10并向上取整得到总页数
						const total = res.data.data.total / that.pageSize;
						that.total2 = Math.ceil(total);
						// that.total = res.data.data.total;
						that.pageIndex = res.data.data.pageNum
						that.pageSize = res.data.data.pageSize;
						that.isLoadingShow = false
					})
				},
				prevOrNext: function(n,szCur) {
					this.pageIndex += n
					this.pageIndex <= 1 ? this.pageIndex = 1 : this.pageIndex > this.total ? this.pageIndex = this.total : null
					if(szCur == 0){
						this.getDigitalProducts();
					}else if(szCur == 1){
						this.getBooksForSales();
					}
				},
				selectPage: function(n,szCur) {
					if(n=='尾页'){
						if(szCur == 0) {
							n = this.total1
						}else if(szCur == 1){
							n = this.total2
						}
					}
					if(n=='首页'){
						n = 1
					}
					if (n === this.pageIndex) return
					if (typeof n === 'string') return
					this.pageIndex = n;
					if(szCur == 0){
						this.getDigitalProducts();
					}else if(szCur == 1){
						this.getBooksForSales();

					}
				},
				handleGoToPage: function(szCur) {
					if(szCur == 0){
						this.pageIndex = this.goToPage <= 1 ? 1 : this.goToPage - 0 >= this.total1 - 0 ? this.total1 :
								this.goToPage
						this.goToPage = this.pageIndex;
						this.getDigitalProducts();
					}else if(szCur == 1){
						this.pageIndex = this.goToPage <= 1 ? 1 : this.goToPage - 0 >= this.total2 - 0 ? this.total2 :
								this.goToPage
						this.goToPage = this.pageIndex;
						this.getBooksForSales();

					}
				},
				downloadFile(fileUrl,title){
					let that = this;
					that.isLoadingShow = true
					axios(fileUrl, {//pdf的位置
						responseType: 'blob', //重要代码
					}).then(res => {
						const url = window.URL.createObjectURL(new Blob([res.data]));
						const link = document.createElement('a');
						link.href = url;
						const str = fileUrl.split(".")
						let fileName = title+"."+str[str.length-1] //保存到本地的文件名称
						link.setAttribute('download', fileName);
						document.body.appendChild(link);
						link.click();
						console.log(link);
						link.remove();
						that.isLoadingShow = false
					})
				},

			}
		};
		Vue.createApp(App).mount('#app');
	</script>

	<style>
		.sales_zone{
			display: flex;
			margin: 0 3rem;
		}
		.sales_zone_nav{
			margin-right: 0.3rem;
		}
		.sales_zone_nav .title{
			width: 2.7rem;
			height: 0.88rem;
			background-image: url("img/back2.png");
			background-size: cover;
			background-repeat: no-repeat;
			background-position: center;
			display: flex;
			align-items: center;
			padding-left: 0.3rem;
			margin: 0 0.01rem;
		}
		.sales_zone_nav .title .p1{
			font-size: 0.24rem;
			color: #fff;
			font-weight: 600;
			letter-spacing: 0.01rem;
			margin-bottom: 0.1rem;
		}
		.sales_zone_nav .title .p2{
			font-size: 0.13rem;
			color: #fff;
			font-weight: 600;
			line-height: 0.12rem;
		}
		.nav_list{
		}
		.nav_list ul li{
			height: 0.7rem;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 0.3rem;
			border: 0.01rem solid #eee;
			border-top: none;
			cursor: pointer;
			
		}
		.nav_list_left{
			display: flex;
			align-items: center;
		}
		.nav_list ul li .img1{
			width: 0.14rem;
			margin-right:0.2rem;
		}
		.nav_list ul li .img2{
			width: 0.07rem;
			margin-left: 0.12rem;
		}
		.nav_list ul li span{
			font-size: 0.17rem;
		}
		
		.sales_zone_cont{
			flex: 1;
			margin-top: 0.2rem;
		}
		.sales_zone_cont_title{
			margin-bottom: 0.2rem;
		}
		.sales_zone_cont_titles{
			display: flex;
			align-items: center;
			margin-bottom: 0.3rem;
		}
		.sales_zone_cont_titles p{
			font-size: 0.30rem;
			font-weight: 600;
		}
		.sales_zone_cont_titles img{
			width: 0.10rem;
			margin-right: 0.1rem;
		}
		.sales_zone_cont_title_back{
			width: 100%;
			height: 0.1rem;
		}
		
		.b3 {
			display: block;
			width: 1.2rem;
			line-height: 0.48rem;
			font-size: 0.16rem;
			font-weight: 400;
			color: #203051;
			padding-left: 0.19rem;
			background: url(./img/a9.png) 0.95rem center no-repeat;
			border: 0.01rem solid #203051;
			cursor: pointer;
			position: absolute;
			right: 0.1rem;
			top: 50%;
			transform: translate(0 ,-50%);
		}
		.code{
			text-align: center;
			padding: 0.3rem 0 1.6rem;
		}
		.code img{
			width: 2.6rem;
			height: 2.6rem;
			margin-bottom: 0.1rem;
		}
		.code p{
			color: #233052;
			font-size: 0.16rem;
		}
		.book_list{
			margin-top: 0.4rem;
		}
		.book_list ul{
			margin-bottom: 0.1rem;
		}
		.book_list ul li{
			border: 0.01rem solid #ddd;
			padding: 0.14rem;
			display: flex;
			align-items: center;
			cursor: pointer;
			margin-bottom: 0.2rem;
		}
		.book_list ul li img{
			width: 1.8rem;
			height: 2.4rem;
			margin-right: 0.3rem;
		}
		.book_list ul li .content{
			flex: 1;
			position: relative;
		}
		.book_list ul li .content .title{
			font-size: 0.22rem;
			margin-bottom: 0.2rem;
			letter-spacing: 0.01rem;
			color: #333;
			font-weight: 600;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}
		.book_list ul li .content .text{
			font-size: 0.17rem;
			letter-spacing: 0.01rem;
			margin-bottom: 0.16rem;
			color: #656565;
		}
		.book_list ul li .content .text span{
			font-size: 0.24rem;
			font-weight: bold;
			letter-spacing: 0.01rem;
			color: #ae0e16;
			margin-right: 0.2rem;
		}
	</style>
</html>
